Skip to content

Add Quick Settings tiles snippets - #1087

Open
hamen wants to merge 1 commit into
android:mainfrom
StellarElements:hamen/quicksettings-tiles-snippets
Open

hamen wants to merge 1 commit into
android:mainfrom
StellarElements:hamen/quicksettings-tiles-snippets

Conversation

@hamen

@hamen hamen commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Code snippets are for: Quick Settings tiles

Three Kotlin blocks, into views/src/main/java/com/example/example/snippet/views/quicksettings/QuickSettingsTilesSnippets.kt.

Region tag Page section Same as the page? Lines snippet/page
android_views_quicksettings_tile_service Create and declare your TileService Yes, formatting aside 25/25
android_views_quicksettings_update_tile Update your tile One line differs, see below 11/11
android_views_quicksettings_handle_taps Handle taps One line differs, see below 9/9

spotlessApply changed indentation from two spaces to four and added a space in class MyQSTileService : TileService(). No line counts changed.

List of modifications

Two blocks on the page do not compile, and each needed one line changed.

Update your tile sets the content description from tile.label, and the Kotlin block declares no tile. Every other line in the block uses qsTile. The Java block on the same page declares Tile tile = getQsTile() and then uses tile, so the Kotlin block reads like a translation that lost the local variable and kept one use of it. Building the published lines gives Unresolved reference 'tile'.

The snippet reads state.label, which is the value assigned to qsTile.label one line earlier, so every right-hand side in the block comes from the model the block declares.

The page can be repaired either way: give the Kotlin block the Tile tile = getQsTile() line its Java sibling already has, or read from state as this snippet does.

Handle taps declares var clicks = 0 and then increments counter four times. Building the published lines gives four Unresolved reference 'counter' errors. The snippet names the declaration counter, which changes one line instead of four. Naming the four uses clicks is equally correct — please use whichever name reads better on the page.

Both blocks need a page edit either way. A reader who copies them today gets a compile error.

How the file is arranged

StateModel sits inside UpdateTileService rather than at the top level. The page shows the data class, a blank line, then the override; two spans of one region tag cannot keep that blank line, and the published snippet would come out a line shorter. One span keeps it. The nesting does not change what the page shows.

getStateFromService() is outside the region tags. The page calls it in both the Kotlin and the Java block and never defines it, so it reads as an app function rather than a platform API. The stub is here only so the module compiles.

The three regions live in three subclasses of TileService, not one. The first block overrides onStartListening() and onClick(), the second overrides onStartListening(), the third overrides onClick(). The second and third cannot share the first class.

Snippets not migrated

No other block on the page carries a region tag. Line numbers are from quicksettings-tiles.md.txt.

Block Line Lines Suggested
Java, Create and declare your TileService 101 32 Retire with the page edit
Java, Update your tile 328 23 Retire with the page edit
Java, Handle taps 379 12 Retire with the page edit
Java, Prompt the user to add your tile 513 7 Keep, see below
Launch an activity 420 3 Your call, see below
XML manifest, Create and declare your TileService 140 10 Not extracted, see below
XML manifest, Active mode (recommended) 223 5 Not extracted
XML manifest, Mark your tile as toggleable 436 4 Not extracted
XML manifest, Implementation 474 7 Not extracted

Keep the block at line 513: it shows the signature of requestAddTileService() and is reference text, not an example a reader copies.

The block at line 420 adds Intent.FLAG_ACTIVITY_NEW_TASK. It sits in the prose rather than under a Kotlin or Java heading, so it reads as an example for both languages, but it carries a semicolon, which makes it Java as written. Removing the semicolon makes it Kotlin and it stays correct for a Java reader. That is a page decision, so nothing here changes it.

The four XML manifest examples are not Kotlin and this change does not extract them. The one at line 140 has a defect worth fixing while the page is open:

android:label="@string/my_default_tile_label"  // 18-character limit.

XML has no // comment, so the example does not parse as printed and a reader who copies it gets a manifest that does not build. Moving the note above the element as <!-- 18-character limit. --> keeps the note and makes the example build. It has no effect on the snippets.

Extracts the three Kotlin blocks from the Quick Settings tiles guide into
views/.../quicksettings/QuickSettingsTilesSnippets.kt.

Two of the three blocks do not compile as published, and each is corrected
by one line:

- Update your tile sets the content description from tile.label, and the
  Kotlin block declares no tile. The Java block on the same page declares
  Tile tile = getQsTile(). The snippet reads state.label instead, so every
  right-hand side in the block comes from the model the block declares.
- Handle taps declares var clicks = 0 and then increments counter four
  times. The snippet names the declaration counter, which changes one line
  rather than four.

Both need a page edit either way. The pull request description says so, and
gives the alternative for each.
@hamen
hamen marked this pull request as ready for review September 11, 2026 16:05
@snippet-bot

snippet-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant